﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IsRotation
{
    public static class StringExtension
    {
        public static bool IsRotation(this string source, string target)
        {
            bool result = false;

            if (source.Length != target.Length)
                return result;

            source += source;

            int i = 0;
            while (i < source.Length - target.Length)
            {
                result = true;

               int j = 0;
               while (j < target.Length)
               {
                   if (source[i] != target[j])
                   {
                       result = false;
                       break;
                   }

                   i++;
                   j++;
               }

               if (result)
                   return result;
               else
                   i++;
            }

            return result;
        }

        public static bool IsRotationOptimized(this string source, string target)
        {
            bool result = false;

            if (source.Length != target.Length)
                return result;

            source += source;

            int i = 0, count = 0;
            while (i < source.Length - target.Length)
            {
                if (source[i] == target[count])
                    count++;
                else
                    count = 0;

                if (count == target.Length)
                    result = true;
            }

            return result;
        }
    }
}
